package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Autowired
    private WmMaterialService wmMaterialService;

    @PostMapping("/upload_picture")
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        log.info("上传图片");
        return wmMaterialService.uploadPicture(multipartFile);
    }

    /**
     * 素材列表查询
     *
     * @param wmMaterialDto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult findList(@RequestBody WmMaterialDto wmMaterialDto) {
        log.info("分页查询: {}", wmMaterialDto);
        return wmMaterialService.findList(wmMaterialDto);
    }

    /**
     * 添加收藏
     *
     * @return
     */
    @GetMapping("/collect/{id}")
    public ResponseResult addCollect(@PathVariable("id") Integer id) {
        log.info("添加收藏; {}",id);
        wmMaterialService.update(Wrappers.<WmMaterial>lambdaUpdate().eq(WmMaterial::getId, id)
                .set(WmMaterial::getIsCollection, 1));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 添加收藏
     *
     * @return
     */
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult delCollect(@PathVariable("id") Integer id) {
        log.info("取消收藏; {}",id);
        wmMaterialService.update(Wrappers.<WmMaterial>lambdaUpdate().eq(WmMaterial::getId, id)
                .set(WmMaterial::getIsCollection, 0));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}

